<template>
  <el-form :inline="true" class="demo-form-inline" :model="cForm">
    <el-form-item label="一级分类">
      <el-select placeholder="活动区域" v-model="cForm.category1id" @change="handler1" :disabled="!show">
        <el-option :label="c1.name" :value="c1.id" v-for="c1 in list" :key="c1.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类">
      <el-select placeholder="活动区域" v-model="cForm.category2id" @change="handler2" :disabled="!show">
        <el-option :label="c2.name" :value="c2.id" v-for="c2 in list1" :key="c2.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="三级分类" >
      <el-select placeholder="活动区域" v-model="cForm.category3id" @change="handler3" :disabled="!show">
        <el-option label="区域一" :value="c3.id" v-for="c3 in list2" :key="c3.id" :label="c3.name"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name:"CategoryList",
  props: ["show"],
  components: {},
  data() {
    return {
      list:{},
      list1:{},
      list2:{},
      cForm:{
        category1id:"",
        category2id:"",
        category3id:""
      }
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getCategoryList()
  },
  methods: {
    async getCategoryList(){
      // 发起获取一级分类列表请求
      const res=await this.$API.attr.reqCategory1List()
      // 如果请求成功就存储数据
      if(res.code==200)
      {
        this.list=res.data
      }
    },
    // 三级列表一级分类改变事件
    async handler1(){
      // 清除数据
      this.list1={}
      this.list2={}
      this.cForm.category2id=""
      this.cForm.category3id=""
      const {category1id}=this.cForm
      this.$emit('getCategoryList',{categoryId:category1id,level:1});
      // console.log(category1id);
      // 发起请求
      const res=await this.$API.attr.reqCategory2List(category1id)
      // console.log(res)
      // 成功则存储数据
      if(res.code==200){
        this.list1=res.data
      }
    },
    // 三级列表二级分类改变事件
    async handler2(){
      // 清除数据
      this.list2={}
      this.cForm.category3id=""
      const {category2id}=this.cForm
      this.$emit('getCategoryList',{categoryId:category2id,level:2});
      // 发起请求
      const res=await this.$API.attr.reqCategory3List(category2id)
      // 成功则存储数据
      if(res.code==200){
        this.list2=res.data
      }
    },
    // 三级列表三级分类改变事件
    async handler3(){
      const {category3id}=this.cForm
      this.$emit('getCategoryList',{categoryId:category3id,level:3});
    }
  },
};
</script>

<style scoped lang="less"></style>
